$(function() {
    if (typeof(Storage) !== "undefined")
    {
        $.post(localStorage.domain_app+"/webservice/toDo/getToDoById.php", {
            todo_id: sessionStorage.todo_edit_id,
            user_id: localStorage.userID

        }, function(res) {
            if (res.error == 0) {
                if (res.title != "") {
                    $('input[name="todo-item"]').val(res.title);
                }
                if (res.start_date != "") {
                    $('input[name="todo-date"]').prop('type', 'date');
                    $('input[name="todo-date"]').val(res.start_date);
                }
                if (res.start_time != "") {
                    $('input[name="start-time"]').prop('type', 'time');
                    $('input[name="start-time"]').val(res.start_time);
                }
                if (res.end_time != "") {
                    $('input[name="end-time"]').prop('type', 'time');
                    $('input[name="end-time"]').val(res.end_time);
                }
            } else {
                print_alert(res.msg);
                wd_redirect("toDoList.html");
            }
        }, "json");

        var btn_delete = false;
        $(".btn-delete-todo").click(function() {
            alert("ok delete");
            if (btn_delete == false) {
                btn_delete = true;
                if (confirm("Are you sure you want to delete this task?")) {
                    var todo_id = sessionStorage.todo_edit_id;
                    var user_id = 0;
                    if (typeof(Storage) !== "undefined")
                    {
                        user_id = localStorage.userID;
                    }
                    $.post(localStorage.domain_app+"/webservice/toDo/deleteToDo.php", {
                        todo_id: todo_id,
                        user_id: user_id
                    }, function(res) {
                        var num_milliseconds = 3000;
                        if (res.error == 1) {
                            $('#error-msg').addClass('active').children().html(res.msg);
                            hideError_2('error-msg', num_milliseconds);

                        } else {
                            print_alert(res.msg);
                            wd_redirect("toDoList.html");
                        }
                    }, "json");
                } else {
                    return false;
                }
            }
            btn_delete = false;

        });

        var btn_reminder = false;
        $(".btn-add-to-phone-reminder").click(function() {
            if (btn_reminder == false) {
                btn_reminder = true;
                var todo_id = sessionStorage.todo_edit_id;
                var user_id = 0;
                if (typeof(Storage) !== "undefined")
                {
                    user_id = localStorage.userID;
                }
                $.post(localStorage.domain_app+"/webservice/toDo/addToPhoneReminder.php", {
                    todo_id: todo_id,
                    user_id: user_id
                }, function(res) {
                    var num_milliseconds = 3000;
                    if (res.error == 1) {
                        $('#error-msg').addClass('active').children().html(res.msg);
                        hideError_2('error-msg', num_milliseconds);

                    } else {
                        var cal = new calendarPlugin();
                        var title = res.title;
                        var location = "";
                        var notes = "";
                        var startDate = res.start_time;
                        var endDate = res.end_time;
                        var errCall = function(theerror) {
                            console.log("Error occurred - " + theerror);
                        }
                        var succCall = function(themessage) {
                            console.log("Success - " + themessage);
                        }

                        cal.createEvent(title, location, notes, startDate, endDate, succCall, errCall);
                    }
                }, "json");

            }
            btn_reminder = false;

        });
        var btn_del_reminder = false;
        $(".btn-del-phone-reminder").click(function() {
            if (btn_del_reminder == false) {
                btn_del_reminder = true;
                var todo_id = sessionStorage.todo_edit_id;
                var user_id = 0;
                if (typeof(Storage) !== "undefined")
                {
                    user_id = localStorage.userID;
                }
                $.post(localStorage.domain_app+"/webservice/toDo/addToPhoneReminder.php", {
                    todo_id: todo_id,
                    user_id: user_id
                }, function(res) {
                    var num_milliseconds = 3000;
                    if (res.error == 1) {
                        $('#error-msg').addClass('active').children().html(res.msg);
                        hideError_2('error-msg', num_milliseconds);

                    } else {
                        var cal = new calendarPlugin();
                        var title = res.title;
                        var location = "";
                        var notes = "";
                        var startDate = res.start_time;
                        var endDate = res.end_time;
                        var errCall = function(e) {
                            console.log("Error occurred - " + e);
                        }
                        var succCall = function(themessage) {
                            console.log("Success " + themessage);

                        }

                        cal.deleteEvent(title, location, notes, startDate, endDate, true, succCall, errCall);
                        print_alert("Delete success");
                    }
                }, "json");

            }
            btn_del_reminder = false;

        });
    }
    else
    {
        return false;
    }

});
